package com.woniu.inlet.web;

import com.woniu.inlet.vo.DataVo;
import com.woniu.inlet.vo.GoldVo;
import com.woniu.outlet.elasticSearch.po.Gold;
import com.woniu.outlet.mysql.po.GoldMysql;
import com.woniu.outlet.redis.po.GoldRela;
import com.woniu.service.goldcommand.GetAllGoldCommand;
import com.woniu.service.goldcommand.GoldDetailCommand;
import com.woniu.service.goldcommand.GoldGraphCommand;
import com.woniu.service.goldcommand.GoldsCommand;
import com.woniu.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: my
 * @time: 2021/4/22 19:48
 */
@Slf4j
@RestController
@CrossOrigin
@Api("黄金数据接口")
public class GoldController {
    private static final String DATE_PATTERN = "yyyy-MM-dd";

    @RequestMapping(value = "/product/gold/allGold",method = {RequestMethod.POST,RequestMethod.GET})
    public List<DataVo> getAllGold(){
        GetAllGoldCommand command = new GetAllGoldCommand();
        List<Gold> list = command.execute();
        List<DataVo> vos = new ArrayList<>();
        for (Gold gold : list) {
            DataVo vo = new DataVo();
            vo.setCode_id(gold.getType());
            vo.setName(gold.getTypename());
            vo.setDay_growth(gold.getChangepercent().toString());
            vo.setToday_valuation(gold.getLastclosingprice());
            vos.add(vo);
        }
        return vos;
    }

    @RequestMapping(value = "/product/gold/detail",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("获取黄金金详情")
    @ApiImplicitParam(name = "type",value = "黄金类型",required = true)
    public ResponseResult getGoldDetail(String type){
        GoldDetailCommand command = new GoldDetailCommand(type);
        try {
            Gold gold = command.execute();
            return new ResponseResult(gold,200);
        } catch (Exception e) {
            log.debug("数据获取失败");
            return ResponseResult.FAIL;
        }
    }

    @ApiOperation("获取黄金种类")
    @RequestMapping(value = "/product/gold/all",method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseResult getAllGoldRela(){
        GoldsCommand command = new GoldsCommand();
        try {
            List<GoldRela> execute = command.execute();
            return new ResponseResult(execute,200);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("数据获取失败");
            return ResponseResult.FAIL;
        }
    }

    @ApiOperation("获取黄金折线图数据")
    @RequestMapping(value = "/product/gold/graph",method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseResult getGoldGraph(String type, Integer month){
        if (month == null || month == 0){
            month = 1;
        }
        if (type == null || "".equals(type.trim())){
            return ResponseResult.FAIL;
        }
        GoldGraphCommand command = new GoldGraphCommand(type,month);
        List<GoldMysql> list = command.execute();
        List<GoldVo> goldVos = new ArrayList<>();
        for (GoldMysql mysql : list) {
            GoldVo vo = new GoldVo();
            vo.setType(mysql.getType());
            vo.setTypename(mysql.getTypename());
            vo.setPrice(mysql.getPrice());
            vo.setTradeamount(mysql.getTradeamount());
            vo.setUpdatetime(mysql.getUpdatetime());
            vo.setChangepercent(mysql.getChangepercent());
            goldVos.add(vo);
        }
        return new ResponseResult(goldVos,200);
    }
}
